package cn.mazexal.exambg.services;

import cn.hutool.core.util.IdUtil;
import cn.mazexal.exambg.common.models.PagedData;
import cn.mazexal.exambg.dataware.ExamPageMapper;
import cn.mazexal.exambg.models.converter.ExamPageConverter;
import cn.mazexal.exambg.models.db.ExamPageDO;
import cn.mazexal.exambg.models.qry.ExamPageQry;
import cn.mazexal.exambg.models.vo.ExamPageVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: Mazexal
 * Date: 2025/5/15
 */
@Service
public class ExamService {

    @Resource
    private ExamPageMapper examPageMapper;

    @Resource
    private ExamPageConverter examPageConverter;

    public PagedData<ExamPageVO> getExamPageList(ExamPageQry qry) {
        List<ExamPageDO> examPageDOList = examPageMapper.selectExamPageList(qry);
        int total = examPageMapper.selectExamPageCount(qry);
        return PagedData.of(qry, total, examPageDOList.stream().map(examPageConverter::toView).toList());
    }

    public ExamPageVO getExamPage(Long id) {
        return examPageConverter.toView(examPageMapper.selectById(id));
    }

    public Boolean addExamPage(ExamPageVO examPageVO) {
        Long now = System.currentTimeMillis();
        ExamPageDO db = examPageConverter.toDb(examPageVO);
        db.setCreateTime(now);
        db.setUpdateTime(now);
        db.setId(IdUtil.getSnowflakeNextId());
        return examPageMapper.insert(db) > 0;
    }

    public Boolean updateExamPage(ExamPageVO examPageVO) {
        ExamPageDO db = examPageConverter.toDb(examPageVO);
        db.setUpdateTime(System.currentTimeMillis());
        return examPageMapper.updateById(db) > 0;
    }

    public Boolean deleteExamPage(Long id) {
        ExamPageDO examPageDO = examPageMapper.selectById(id);
        examPageDO.setIsDel(1);
        examPageDO.setUpdateTime(System.currentTimeMillis());
        return examPageMapper.updateById(examPageDO) > 0;
    }
}
